// pages/appoint/appoint.js
import {$http} from './../../../utils/util'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: -1,
    disabled: true,
    execute: {
      dayShift: {
        type: 1,
        enabled: false, // enabled指的是有没有计划被启用
        active: false, // active指的是被启用的计划有没有执行
        startTime: null,
        endTime: null,
        remain: 0
      },
      nightShift: {
        type: 2,
        enabled: false,
        active: false,
        startTime: null,
        endTime: null,
        remain: 0
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 1.设置屏幕高度
    this.setHeight();
  },
  onShow: function() {
    // 2.加载计划信息
    this.loadExecute();
  },
  /**
   * 获取正在执行的计划信息
   */
  loadExecute() {
    $http.get({
      url: '/getCurrentExecute',
      success: res => { 
        if (res.data.code === 200) {
          this.setData({
            execute: res.data.data
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络出错',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },
  /**
   * 设置屏幕高度
   */
  setHeight() {
    let {windowHeight} = wx.getSystemInfoSync();
    this.setData({
      // 使用自定义导航栏时页面的高度
      // windowHeight: (windowHeight - wx.getStorageSync('statusBarHeight')
      //     - wx.getStorageSync('navigationBarHeight')) + 'px',
      // 不使用自定义导航栏时页面的高度
      windowHeight: windowHeight + 'px'
    })
  },
  /**
   * 对点击的时间段对应的盒子加上`active`类名
   * @param event
   */
  onClickActiveType(event) {
    let active = -1;
    if (event.target.dataset.index === "0") {
      active = "0";
    } else if (event.target.dataset.index === "1") {
      active = "1";
    } else if (event.target.dataset.index === "2") {
      active = "2";
    }
    this.setData({ 
      active 
    })
    if ((active === '0' && this.data.execute.dayShift.active === '已开启' && this.data.execute.dayShift.remain !== 0)
        || (active === '1' && this.data.execute.nightShift.active === '已开启' && this.data.execute.nightShift.remain !== 0)
        || (active === '2' && this.data.execute.today.active === '已开启' && this.data.execute.today.remain !== 0)) {
      this.setData({
        disabled: false
      })
    } else {
      this.setData({
        disabled: true
      })
    }
  },
  /**
   * 点击确认按钮跳转到真正预约的界面
   */
  onClickToAppoint() {
    if (this.data.active === '0') {
      wx.setStorageSync('execute', this.data.execute.dayShift)
    } else if (this.data.active === '1') {
      wx.setStorageSync('execute', this.data.execute.nightShift)
    } else if (this.data.active === '2') {
      wx.setStorageSync('execute', this.data.execute.today)
    }
    wx.navigateTo({
      url: '/pages/appoint/appoint/appoint'
    })
  }
})